package com.yuzhitong.shapi.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.activity.AgreementWebActivity;
import a.a.a.MyApplication;
import com.yuzhitong.shapi.base.Contents;

/**
 * Created by Brsya
 * CreateDate: 2022/4/19  16:30
 * Description:
 * 隐私协议弹框
 */
public class AgreementDialog extends CenterDialog implements View.OnClickListener {
    private Context context;
    private View rootView;
    private TextView textAgreement;
    private TextView textCancel;
    private TextView textConfirm;


    private OnClickBtnListener onClickBtnListener;

    public AgreementDialog(Context context) {
        super(context);
        this.context = context;

        setContentView(rootView = LayoutInflater.from(context).inflate(R.layout.dialog_agreement, null, false));

        initView();
    }

    private void initView() {
        setKeyListener(false);

        textAgreement = (TextView) rootView.findViewById(R.id.text_agreement);
        textCancel = (TextView) rootView.findViewById(R.id.text_cancel);
        textConfirm = (TextView) rootView.findViewById(R.id.text_confirm);

        SpannableString spannableString = new SpannableString(context.getString(R.string.agreement_hint));
        // 17 23 29 TODO
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 打开用户协议
                Intent intent = new Intent(context, AgreementWebActivity.class);
                intent.putExtra(Contents.INTENT_H5_AGREEMENT, Contents.AGREEMENT_URL_USER);
                intent.putExtra(Contents.INTENT_TITLE_AGREEMENT, context.getString(R.string.agreement_title_user));
                context.startActivity(intent);
            }

            //去除连接下划线
            @Override
            public void updateDrawState(TextPaint ds) {
                /**set textColor**/
                ds.setColor(Color.BLUE);
                /**Remove the underline**/
                ds.setUnderlineText(false);
            }

        }, 39, 45, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 打开隐私协议
                Intent intent = new Intent(context, AgreementWebActivity.class);
                intent.putExtra(Contents.INTENT_H5_AGREEMENT, Contents.AGREEMENT_URL_PRIVATE);
                intent.putExtra(Contents.INTENT_TITLE_AGREEMENT, context.getString(R.string.agreement_title_private));
                context.startActivity(intent);
            }

            //去除连接下划线
            @Override
            public void updateDrawState(TextPaint ds) {
                /**set textColor**/
                ds.setColor(Color.BLUE);
                /**Remove the underline**/
                ds.setUnderlineText(false);
            }
        }, 45, 51, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textAgreement.setMovementMethod(LinkMovementMethod.getInstance());
        textAgreement.setText(spannableString);

        textCancel.setOnClickListener(this);
        textConfirm.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.text_cancel) {
            dismiss();
            if (onClickBtnListener != null) {
                onClickBtnListener.onDismiss();
            }
        } else if (id == R.id.text_confirm) {
            MyApplication.agreeIsTrue();
            dismiss();
            if (onClickBtnListener != null) {
                onClickBtnListener.onConfirm();
            }
        }
    }


    public void setOnClickBtnListener(OnClickBtnListener onClickBtnListener) {
        this.onClickBtnListener = onClickBtnListener;
    }

    public interface OnClickBtnListener {
        void onDismiss();

        void onConfirm();
    }
}
